package ro.gateway.aida.obj.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Hashtable;

import ro.gateway.aida.db.DBPersistenceManager;
import ro.gateway.aida.db.PersistenceToken;


/**
 * @author Mihai Postelnicu
 */
public class SystemPropertyDB extends DBPersistenceManager {

      private SystemPropertyDB ( PersistenceToken token ) {
            super( token );
      }

      public static SystemPropertyDB getManager ( PersistenceToken token ) {
            return new SystemPropertyDB( token );
      }

      public Hashtable getAll () throws SQLException {
            Connection con = getConnection();
            Hashtable ret = new Hashtable();

            try {
                  PreparedStatement ps = con.prepareStatement( GET_ALL );
                  ResultSet rs = ps.executeQuery();

                  while ( rs.next() )
                        ret.put( rs.getString( 1 ), rs.getString( 2 ) );

                  rs.close();
                  ps.close();
            } finally {
                  con.close();
            }

            return ret;
      }

      public String getProperty ( String name ) throws SQLException {
            Connection con = getConnection();
            String ret = null;

            try {
                  PreparedStatement ps = con.prepareStatement( GET_PROPERTY );
                  ps.setString( 1, name );

                  ResultSet rs = ps.executeQuery();

                  if ( rs.next() )
                        ret = rs.getString( 1 );

                  rs.close();
                  ps.close();
            } finally {
                  con.close();
            }

            return ret;
      }

      public void setProperty ( String name, int value ) throws SQLException {
            setProperty( name, Integer.toString( value ) );
      }

      public void setProperty ( String name, String value )
            throws SQLException {
            Connection con = getConnection();

            int x = 0;
            try {
                  PreparedStatement ps = con.prepareStatement( SET_PROPERTY );
                  ps.setString( 1, value );
                  ps.setString( 2, name );
                  x = ps.executeUpdate();
                  ps.close();
            } finally {
                  con.close();
            }
            if ( x == 0 ) {
                  insertProperty( name, value );
            }
      }

      private void insertProperty ( String name, String value ) throws SQLException {
            Connection con = getConnection();
            try {
                  PreparedStatement ps = con.prepareStatement( INSERT_PROPERTY );
                  ps.setString( 1, value );
                  ps.setString( 2, name );
                  ps.executeUpdate();
                  ps.close();
            } catch ( SQLException slqEx ) {
                  throw slqEx;
            } finally {
                  con.close();
            }
      }

      private static final String GET_PROPERTY = "SELECT value FROM system_properties WHERE name=?";
      private static final String SET_PROPERTY = "UPDATE system_properties SET value=? WHERE name=?";
      private static final String INSERT_PROPERTY = "INSERT INTO system_properties (value, name) VALUES (?,?)";
      private static final String GET_ALL = "SELECT name,value FROM system_properties";
}
